package com.ruoyi.project.system.service.impl;

import java.util.*;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.system.domain.CollegeStaff;
import com.ruoyi.project.system.domain.SafetyHazardRecord;
import com.ruoyi.project.system.domain.Student;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.mapper.CollegeStaffMapper;
import com.ruoyi.project.system.mapper.SafetyHazardRecordMapper;
import com.ruoyi.project.system.mapper.StudentMapper;
import com.ruoyi.project.system.service.ISafetyHazardRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 安全风险记录Service业务层处理
 *
 * @author ruoyi
 * @date 2020-10-07
 */
@Service
public class SafetyHazardRecordServiceImpl implements ISafetyHazardRecordService {
    @Autowired
    private SafetyHazardRecordMapper safetyHazardRecordMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private CollegeStaffMapper collegeStaffMapper;

    /**
     * 查询安全风险记录
     *
     * @param id 安全风险记录ID
     * @return 安全风险记录
     */
    @Override
    public SafetyHazardRecord selectSafetyHazardRecordById(Integer id) {
        return safetyHazardRecordMapper.selectSafetyHazardRecordById(id);
    }

    /**
     * 查询安全风险记录列表
     *
     * @param safetyHazardRecord 安全风险记录
     * @return 安全风险记录
     */
    @Override
    public List<SafetyHazardRecord> selectSafetyHazardRecordList(SafetyHazardRecord safetyHazardRecord) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        int userType = loginUser.getUser().getType();
        if (userType == 2) {
            safetyHazardRecord.setCollegeId(loginUser.getCollegeStaff().getCollegeId());
        } else if(userType ==1) {
            safetyHazardRecord.setStudentId(loginUser.getStudent().getId().longValue());
        }
        return safetyHazardRecordMapper.selectSafetyHazardRecordList(safetyHazardRecord);
    }

    /**
     * 新增安全风险记录
     *
     * @param safetyHazardRecord 安全风险记录
     * @return 结果
     */
    @Override
    public int insertSafetyHazardRecord(SafetyHazardRecord safetyHazardRecord) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        if (loginUser.getUser().getType() == 1) {
            Student student =loginUser.getStudent();
            safetyHazardRecord.setCreateDt(new Date());
            safetyHazardRecord.setCollegeId(student.getCollegeId().longValue());
            safetyHazardRecord.setCollegeName(student.getCollegeName());
            safetyHazardRecord.setStudentId(student.getId().longValue());
            safetyHazardRecord.setStudentName(student.getRealName());
            safetyHazardRecord.setPhone(student.getCellphone());
        } else {
            throw new CustomException("教师暂无新增数据权限！");
        }
        return safetyHazardRecordMapper.insertSafetyHazardRecord(safetyHazardRecord);
    }

    /**
     * 修改安全风险记录
     *
     * @param safetyHazardRecord 安全风险记录
     * @return 结果
     */
    @Override
    public int updateSafetyHazardRecord(SafetyHazardRecord safetyHazardRecord) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        CollegeStaff collegeStaff = loginUser.getCollegeStaff();
        safetyHazardRecord.setCollegeStaffId(collegeStaff.getCollegeId());
        safetyHazardRecord.setCollegeStaffName(collegeStaff.getName());
        safetyHazardRecord.setDealDt(new Date());
        safetyHazardRecord.setState(2);
        return safetyHazardRecordMapper.updateSafetyHazardRecord(safetyHazardRecord);
    }

    /**
     * 批量删除安全风险记录
     *
     * @param ids 需要删除的安全风险记录ID
     * @return 结果
     */
    @Override
    public int deleteSafetyHazardRecordByIds(Integer[] ids, String dealContent,SysUser sysUser) {
        int i = 0;
        for (Integer id : ids) {

            SafetyHazardRecord safetyHazardRecord = new SafetyHazardRecord();
            safetyHazardRecord.setId(id);
            safetyHazardRecord.setDealDt(new Date());
            int type = sysUser.getType();
            if(type==2){
                CollegeStaff collegeStaff = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest()).getCollegeStaff();
                safetyHazardRecord.setCollegeStaffId(collegeStaff.getId().longValue());
                safetyHazardRecord.setCollegeStaffName(collegeStaff.getName());
            }else{
                safetyHazardRecord.setCollegeStaffId(sysUser.getUserId());
                safetyHazardRecord.setCollegeStaffName(sysUser.getUserName());
            }
            safetyHazardRecord.setState(2);
            safetyHazardRecord.setDealContent(dealContent);
            i++;
            safetyHazardRecordMapper.updateSafetyHazardRecord(safetyHazardRecord);
        }

        return i;
    }

    /**
     * 删除安全风险记录信息
     *
     * @param id 安全风险记录ID
     * @return 结果
     */
    @Override
    public int deleteSafetyHazardRecordById(Integer id) {
        return safetyHazardRecordMapper.deleteSafetyHazardRecordById(id);
    }

    /**
     * 获取待处理和已处理数据
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getSelectlist(SysUser sysUser) {
        List<Map<String, Object>> list = new ArrayList<>();

        if (sysUser.getUserId() == 1) {


            int listDcl = safetyHazardRecordMapper.selectList(1);
            Map<String, Object> mapdcl = new HashMap<>();
            mapdcl.put("name", "待处理数据（" + listDcl + ")");
            mapdcl.put("id", 1);
            list.add(mapdcl);

            int listYcl = safetyHazardRecordMapper.selectList(2);
            Map<String, Object> mapycl = new HashMap<>();
            mapycl.put("name", "已处理数据（" + listYcl + ")");
            mapycl.put("id", 2);
            list.add(mapycl);

        } else {

            int listDcl = safetyHazardRecordMapper.selectThList(sysUser.getDeptId(), 1);
            Map<String, Object> mapdcl = new HashMap<>();
            mapdcl.put("name", "待处理数据（" + listDcl + ")");
            mapdcl.put("id", 1);
            list.add(mapdcl);


            int listYcl = safetyHazardRecordMapper.selectThList(sysUser.getDeptId(), 2);
            Map<String, Object> mapycl = new HashMap<>();
            mapycl.put("name", "已处理数据（" + listYcl + ")");
            mapycl.put("id", 2);
            list.add(mapycl);
        }

        return list;
    }
}
